<template>
  <div class="producttype">
    <h3>
      <span>商品分类</span>
      <el-button
        :disabled="showdis"
        type="primary"
        class="addtype"
        @click="dialogFormVisible = true"
        >添加分类</el-button
      >
    </h3>
    <el-table
      :data="prolist"
      style="border-radius: 4px; box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1)"
    >
      <el-table-column type="index" label="序号" width="180"></el-table-column>
      <el-table-column label="分类名称">
        <!-- template设置当行普通文本和input框 -->
        <template slot-scope="data">
          <!-- 下面设置了这个标杆editchange有个布尔值 -->
          <el-input
            v-show="data.row.editchange"
            v-model="data.row.cateName"
          ></el-input>
          <!-- {{}}获取动态数据 -->
          <label v-show="!data.row.editchange">{{ data.row.cateName }}</label>
        </template>
      </el-table-column>
      <el-table-column label="是否启用">
        <!-- template里面的slote-scope='形参' 可以动态获取当前数据 -->
        <template slot-scope="data">
          <!-- 形参.row 获取当前行数数据 -->
          <el-switch
            v-model="data.row.state"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :disabled="!data.row.editchange"
          >
            <!-- :disabled动态获取布尔值数据 -->
          </el-switch></template
        >
      </el-table-column>
      <!--  slot-scope="row" -->

      <el-table-column
        label="操作"
        width="180px
      "
        ><template slot-scope="row">
          <el-button
            :disabled="changenow"
            v-show="!row.row.editchange"
            @click="changelist(row.row)"
            >编辑</el-button
          >
          <el-button
            style="margin: 0"
            type="success"
            v-show="row.row.editchange"
            @click="clickfinished(row)"
            >完成</el-button
          >
          <!--  @click="dellist(row)" -->
          <el-button type="danger" @click="dellist(row.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="nowpage"
        :page-size="showpage"
        layout="total, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <!-- 添加分类 -->
    <el-dialog title="添加分类" width="30%" :visible.sync="dialogFormVisible">
      <div class="add-input">
        <span>分类名称</span>
        <div>
          <el-input v-model="proinput" placeholder="分类名称"></el-input>
        </div>
      </div>
      <div class="add-input">
        <span>是否启用</span>
        <el-switch
          v-model="open"
          active-color="#13ce66"
          inactive-color="#ff4949"
        >
        </el-switch>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="clicksure">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 引入获取分类的ajax
import { getlist, addlist, delcate, editcate } from "@/apis/items";
export default {
  data() {
    return {
      // 设定禁用
      showdis: false,
      changenow: false,
      showinner: "",
      showopen: false,
      shownow: false,
      proinput: "",
      open: true,
      dialogFormVisible: false,
      treelist: [
        {
          namestype: "热销榜",
        },
        {
          namestype: "双人精彩套餐",
        },
        {
          namestype: "冰爽饮品限时特惠",
        },
        {
          namestype: "精选菜品",
        },
        {
          namestype: "热销招牌菜",
        },
      ],
      // 当前在第几页
      nowpage: 1,
      // 当前页面显示多少条
      showpage: 5,
      // 分页条数
      total: 0,
      prolist: [],
    };
  },
  methods: {
    // 当前移动到哪个界面
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      // 先赋值给动态数据
      // console.log(val);
      this.nowpage = val;
      // 再调用函数,这时函数的参数等于赋值后的动态数据,所以会转换
      this.fungetlist();
    },
    // 封装一个函数，发送ajax并渲染页面
    fungetlist() {
      // 发送ajax请求,获取后台商品列表数据
      getlist({
        currentPage: this.nowpage,
        pageSize: this.showpage,
      }).then((res) => {
        // console.log(res);
        // 循环获得的数组判定当前status状态值, 是否是1;
        for (const statusnow of res.data.data) {
          // console.log(statusnow.state);
          // 是1的话,==结果为布尔值,就赋给前面
          statusnow.state = statusnow.state == 1;

          // **重要** 每次循环的时候,给当前的数据添加设定一个flag为false
          statusnow.editchange = false;
        }
        this.total = res.data.total;
        this.prolist = res.data.data;
      });
    },
    clicksure() {
      this.showdis = true;
      setTimeout(() => {
        this.showdis = false;
      }, 1000);
      this.dialogFormVisible = false;
      // console.log(this.proinput, this.open);
      // 发送添加商品列表ajax
      addlist({
        cateName: this.proinput,
        state: this.open,
      }).then((res) => {
        // console.log(res);
        if (res.data.code == 0) {
          this.$message({
            message: "恭喜你，添加商品分类成功",
            type: "success",
          });
          this.proinput = "";
          this.fungetlist();
        } else {
          this.$message({
            message: "警告哦，添加商品分类失败",
            type: "warning",
          });
        }
      });
    },
    dellist(i) {
      // console.log(i);
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delcate({
            id: i,
          }).then((res) => {
            // console.log(res);
            if (res.data.code == 0) {
              this.$message({
                message: "恭喜你，删除商品成功",
                type: "success",
              });
              this.fungetlist();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 点击编辑,将标杆设为true
    changelist(i) {
      // 显示框
      i.editchange = true;
    },
    // 点击完成之后,发送ajax改变数据并再次渲染页面
    clickfinished(row) {
      this.changenow = true;
      setTimeout(() => {
        this.changenow = false;
      }, 1000);
      editcate({
        id: row.row.id,
        cateName: row.row.cateName,
        state: row.row.state,
      }).then((res) => {
        // console.log(res);
        // 发送ajax请求成功的话
        if (res.data.code == 0) {
          // 修改flag为false
          row.row.editchange = false;
          this.$message({
            message: "恭喜你，修改商品成功",
            type: "success",
          });
          // 调用刷新页面
          this.fungetlist();
        }
      });
    },
  },
  created() {
    this.fungetlist();
  },
};
</script>

<style lang="less" scoped>
.producttype {
  margin: 20px;
  background-color: #fff;
  box-sizing: border-box;
  padding: 0 20px 20px 20px;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  h3 {
    line-height: 60px;
    border-bottom: 1px solid #ccc;
    margin-bottom: 20px;
    display: flex;
    justify-content: space-between;
    .addtype {
      height: 40px;
      margin-top: 10px;
    }
  }
  .block {
    margin-top: 20px;
  }
  .add-input {
    display: flex;
    line-height: 40px;
    align-items: center;
    margin-bottom: 20px;
    span {
      width: 100px;
    }
  }
}
</style>